<?php
include 'template/header.html';
require_once 'lib/function.php';

    $action = isset($_GET['action'])?$_GET['action']:'list';
    switch ($action) {
        case 'list':
            $sql = "select * from customertype";
            $rowNum = getRowNum($sql);
            // echo $rowNum;
            $page = isset($_GET['page'])?$_GET['page']:1;
            $listNum = 4;
            $maxPage = ceil($rowNum/$listNum);
            $res = page($page,$sql, $listNum);
            // var_dump($res);
            include 'template/customer_type_list.html';

            break;
        case 'add':
            include 'template/customer_type_add.html';
            break;
        case 'add_check':

            $type = $_POST['type'];
            $sql = "select * from customertype where ctype_info = '$type'";
            $res = getRowNum($sql);
            if($res>0){
                echo "<script>alert('已有的类型！');location.href='ctype.php?action=add'</script>";
            }
            $sql = "insert into customertype values('','$type')";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('添加成功!');location.href='ctype.php'</script>";
            }
            break;
        case 'delete':
            $ctype_id = $_GET['ctype_id'];
            $sql = "delete from customertype where ctype_id='$ctype_id'";
            // echo $sql;
            // exit;
            $res = dclData($sql);
            if($res){
                echo "<script>alert('删除成功!');location.href='ctype.php'</script>";
            }
            break;
    }
    include 'template/foot.html';